package io.renren.modules.work.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.work.dao.ScExpenseDao;
import io.renren.modules.work.entity.ScExpensedtlEntity;
import io.renren.modules.work.service.ScExpensedtlService;
import io.renren.vo.ExpenseVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.work.entity.ScExpenseEntity;
import io.renren.modules.work.service.ScExpenseService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 报销单
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2022-05-24 22:24:59
 */
@RestController
@RequestMapping("work/scexpense")
public class ScExpenseController {
    @Autowired
    private ScExpenseService scExpenseService;
    @Autowired
    private ScExpensedtlService scExpensedtlService;
    @Autowired
    private ScExpenseDao scExpenseDao;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("work:scexpense:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = scExpenseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{expenseid}")
    @RequiresPermissions("work:scexpense:info")
    public R info(@PathVariable("expenseid") String expenseid){
        ExpenseVo expenseVo = scExpenseDao.selectExpenseVoById(expenseid);
        return R.ok().put("scExpense", expenseVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("work:scexpense:save")
    public R save(@RequestBody ScExpenseEntity scExpense){
        scExpense.setCretime(new Date());
		scExpenseService.save(scExpense);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("work:scexpense:update")
    public R update(@RequestBody ScExpenseEntity scExpense){
		scExpenseService.updateById(scExpense);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("work:scexpense:delete")
    public R delete(@RequestBody String[] expenseids){
		scExpenseService.removeByIds(Arrays.asList(expenseids));
        for (String expendseid : expenseids) {
            QueryWrapper<ScExpensedtlEntity> scExpensedtlEntityQueryWrapper = new QueryWrapper<>();
            scExpensedtlEntityQueryWrapper.eq("expendseid",expendseid);
            scExpensedtlService.remove(scExpensedtlEntityQueryWrapper);
        }
        return R.ok();
    }

    /**
     * 新增（app)
     */
    @RequestMapping("mSave")
    public R mSave(@RequestBody String params){
        return scExpenseService.mSave(params);
    }

    /**
     * 修改（app）
     */
    @RequestMapping("mUpdate")
    public R mUpdate(@RequestBody String params){
        return scExpenseService.mUpdate(params);
    }

    /**
     * 信息（app）
     */
    @RequestMapping("mInfo")
    public R mInfo(@RequestParam String expenseid){
        return scExpenseService.mInfo(expenseid);
    }
}
